<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/login.png" alt="">
        <span class="blog-title">生活点滴志</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">分享生活</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>

            <div class="row">
                <button id="signIn" onclick="signIn()">注册</button>
            </div>

        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        /*
        token的存储方式有以下三种：
        1. 存储到cookie
        2. 存储到url
        3. 存储到本地中
        */
        function login() {
            $.ajax({
                type: "post",
                url: "/user/login",
                data: {
                    "userName": $("#username").val(),
                    "password": $("#password").val(),
                },
                success: function(result) {
                    console.log(result.data);
                    console.log(result.code);
                    if(result.code == 200 && result.data != null) {
                        //存储token
                        localStorage.setItem("user_token", result.data);
                        location.href = "blog_list.html";
                    }else {
                        console.log(result);
                        alert("用户名或密码错误");
                        //清除输入框中的账号和密码
                        $("#username").val('');
                        $("#password").val('');
                        return;
                    }
                }
            });
        }

        function signIn() {
            location.href = "blog_signIn.html";
        }

    </script>
</body>

</html>